import { Component, Injector } from '@angular/core';
import { NavController, Platform, App, ModalController, Loading, NavParams, AlertController, MenuController, LoadingController, ToastController, ViewController, ActionSheetController } from 'ionic-angular';
import { LazyLoadImageModule } from 'ng-lazyload-image';
import { StorageService } from "../providers/StorageService";
import { DataService } from '../providers/DataService';
import { IonicPage } from 'ionic-angular';
import { Events } from 'ionic-angular';
import { Http, Response, URLSearchParams } from '@angular/http';

@Component({
  selector: 'page-base',
  template: '',
  providers: []
})
export class BasePage {
  get loadingCtrl(): LoadingController { return this.injector.get(LoadingController); };
  get navParams(): NavParams { return this.injector.get(NavParams); };
  get navCtrl(): NavController { return this.injector.get(NavController); };
  get toastCtrl(): ToastController { return this.injector.get(ToastController); };
  get menuCtrl(): MenuController { return this.injector.get(MenuController); };
  get modalCtrl(): ModalController { return this.injector.get(ModalController); };
  get viewCtrl(): ViewController { return this.injector.get(ViewController); };
  get platform(): Platform { return this.injector.get(Platform) };
  get actionCtrl(): ActionSheetController { return this.injector.get(ActionSheetController); };


  //get events(): Events { return this.injector.get(Events) }

  //get app(): App { return this.injector.get(App) }
  //get nativePageTransitions(): any { return this.injector.get(PageFilpService).getFilpObj(); }

  /**
   * 所以注册Events事件的时候必须在界面销毁的时候把事件进行注销,不然下次调用则会重复执行Events中的注册的方法。
    */
  //public events: Events;
  public storageService: StorageService;
  /** 全局共享数据类 */
  public dataService: DataService;
  get alertCtrl(): AlertController { return this.injector.get(AlertController); };

  public title = '美甲就业系统'
  //懒加载
  public defaultImage = "http://" + window.location.host + '/assets/img/loading.png';

  loader: Loading;
  /** 当前用户vo */
  currUserVo = {
    "user_id": '', //用户id''
    "id": "", //身份id
    "user_type": "1",  //1推荐人 2美甲师 3美甲店主
    "name": "", //姓名
    "avator": ''
  };
  constructor(public injector: Injector) {

    // this.menuCtrl.enable(false, 'myMenu');
    // this.menuCtrl.swipeEnable(false);
    // this.storageService = this.injector.get(StorageService);
    // this.dataService = this.injector.get(DataService);
    // document.title = this.title;
    // this.events = new Events();
    // let isLogined: boolean = this.dataService.getData('isLogined');
    // if (!isLogined) {
    //   this.navCtrl.goToRoot({});
    //   return;
    // }
    // this.currUserVo = this.dataService.getData('userVo');

  }


  /** 每次都调用 */
  ionViewWillEnter() {
    //是否是用户直接刷新了页面

    // if (!this.currUserVo)
    //   this.navCtrl.goToRoot({});
  }
  /**
   * 显示加载进度条
   */
  showLoading(content: string = "Please wait...") {
    this.loader = this.loadingCtrl.create({
      content: content,
      duration: 0
    });

    this.loader.present();
  }

  /**
   * 关闭加载进度条
   */
  closeLoading() {
    this.loader.dismiss();
  }

  /**
   *  显示提示
   * @param msg   
   * @param title 
   * @param callFunc 
   */
  showAlert(msg, title = '提示', callFunc = null) {
    let alert = this.alertCtrl.create({
      title: title,
      subTitle: msg,
      buttons: [
        {
          text: "确定",
          handler: data => {
            if (callFunc) {
              callFunc.apply(data);
            }

          }
        }]
    });
    alert.present();
  }

  showToast(msg, duration = 1500, position: string ) {
    let toast = this.toastCtrl.create({
      message: msg,
      duration: 1500,
      position:position,
      cssClass: "toast-success"
    });
    toast.present();
  }

  /**
   * 显示一个确认，取消的弹出框
   * @param msg 
   * @param title 
   * @param callFunc 
   */
  showConfirm(msg, title = "提示", callFunc: any = null) {
    let confirm = this.alertCtrl.create({
      title: title,
      message: msg,
      buttons: [

        {
          text: '取消',
          handler: () => {
            console.log('Agree clicked');
          }
        },
        {
          text: '确定',
          handler: data => {
            if (callFunc)
              callFunc.apply(data);
          }
        },
      ]
    });
    confirm.present();
  }

  ionViewWillLeave = () => {

    // this.nativePageTransitions.slide(this.options)
    //   .then((msg) => {   //onSuccess

    //   })
    //   .catch((msg) => {  //onError

    //   });

  }

  openPage = (page: any, param = null) => {
    // this.nativePageTransitions.slide(this.options);
    // this.navCtrl.push(page, null);
  }

  /** 动态建立 actionsheetcontrol 按钮
   *  openMenu('标题',按钮数组:{id:后台的xx,text:'经费' },this.xxxfunc)
   *  **/
  
  // openMenu(_title, _btnArr, _func = null) {
  //   let actionSheet = this.actionCtrl.create({
  //     title: _title,
  //     buttons: this.createButtons(_btnArr, _func)

  //   });
  //   actionSheet.present();
  // }

  private createButtons(_btnArr, _func) {
    let buttons = [];
    for (let index in _btnArr) {
      let button = {
        text: _btnArr[index].text,
        // icon: _btnArr[index].icon,
        handler: () => {
          _func(_btnArr[index].id);
          console.log('setting icon ' + _btnArr[index].id);
          return true;
        }
      }
      buttons.push(button);
    }
    return buttons;
  }



  /** 打开textarea 文本编辑 
     * _title  弹出窗的标题
     * _str  大文本框里的内容 新增的为 ''
     * _func = 回调的方法
    */
  public openTextArea(_title, _str, _func: any = null) {
    let modal = this.modalCtrl.create("TextareaPage", { title: _title, str: _str });
    modal.onDidDismiss(txtStr => {
      if (txtStr.length == 0) {
        this.showAlert("请添加文字", "提示");
        return;
      }
      if (_func)
        _func(txtStr);
    }
    );
    modal.present();
  }

  /** 打开input文本框
   * _type:文本框类型
   * _inputname：文本框的名称
   * _field：文本框的字段名
   * _fontnum:文本框文字显示的个数
   * _func = 回调的方法
   */
  openInputText(_type, _inputname, _placeholder, _fontnum, _inputvalue, _func: any = null) {
    let param = { 'type': _type, 'inputname': _inputname, 'placeholder': _placeholder, 'fontnum': _fontnum, 'inputvalue': _inputvalue };
    let modal = this.modalCtrl.create('InputtextPage', param);
    modal.onDidDismiss(data => {
      console.log(data);
      if (data == '') {
        this.showAlert('不能为空', '提示');
        return;
      }
      if (_func) {
        _func(data);
        _func(data);
      }
    });
    modal.present();

  }





}